import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import "components"
import "styles"

Rectangle {
    id: timedSamplingPage
    width: 1024
    height: 600
    color: AppStyle.backgroundColor

    property bool isSampling: false
    property int samplingDuration: 0
    property int elapsedTime: 0

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 20

        // 标题
        Text {
            text: "定时采样"
            font.pixelSize: 24
            font.bold: true
            color: AppStyle.textColor
            Layout.alignment: Qt.AlignHCenter
        }

        // 参数设置区域
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 200
            color: AppStyle.cardBackgroundColor
            radius: 8
            border.color: AppStyle.borderColor
            border.width: 1

            GridLayout {
                anchors.fill: parent
                anchors.margins: 20
                columns: 2
                columnSpacing: 20
                rowSpacing: 15

                Text {
                    text: "采样时间 (分钟):"
                    font.pixelSize: 14
                    color: AppStyle.textColor
                }

                SpinBox {
                    id: durationSpinBox
                    from: 1
                    to: 1440 // 24小时
                    value: 10
                    enabled: !timedSamplingPage.isSampling
                    Layout.preferredWidth: 120
                }

                Text {
                    text: "流量 (L/min):"
                    font.pixelSize: 14
                    color: AppStyle.textColor
                }

                SpinBox {
                    id: flowRateSpinBox
                    from: 1
                    to: 100
                    value: 28
                    enabled: !timedSamplingPage.isSampling
                    Layout.preferredWidth: 120
                }

                Text {
                    text: "采样位置:"
                    font.pixelSize: 14
                    color: AppStyle.textColor
                }

                ComboBox {
                    id: locationComboBox
                    model: ["洁净室A", "洁净室B", "洁净室C", "走廊", "其他"]
                    enabled: !timedSamplingPage.isSampling
                    Layout.preferredWidth: 200
                }

                Text {
                    text: "备注:"
                    font.pixelSize: 14
                    color: AppStyle.textColor
                }

                CustomTextField {
                    id: remarksField
                    Layout.fillWidth: true
                    placeholderText: "输入备注信息"
                    enabled: !timedSamplingPage.isSampling
                }
            }
        }

        // 状态显示区域
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 150
            color: AppStyle.cardBackgroundColor
            radius: 8
            border.color: AppStyle.borderColor
            border.width: 1

            ColumnLayout {
                anchors.centerIn: parent
                spacing: 15

                Text {
                    text: timedSamplingPage.isSampling ? "采样进行中" : "待机状态"
                    font.pixelSize: 18
                    font.bold: true
                    color: timedSamplingPage.isSampling ? AppStyle.successColor : AppStyle.textColor
                    Layout.alignment: Qt.AlignHCenter
                }

                ProgressBar {
                    id: progressBar
                    Layout.preferredWidth: 300
                    Layout.alignment: Qt.AlignHCenter
                    from: 0
                    to: timedSamplingPage.samplingDuration * 60 // 转换为秒
                    value: timedSamplingPage.elapsedTime
                    visible: timedSamplingPage.isSampling
                }

                Text {
                    text: timedSamplingPage.isSampling ? 
                          "已用时: " + Math.floor(timedSamplingPage.elapsedTime / 60) + ":" + 
                          String(timedSamplingPage.elapsedTime % 60).padStart(2, '0') + 
                          " / " + timedSamplingPage.samplingDuration + ":00" : ""
                    font.pixelSize: 14
                    color: AppStyle.secondaryTextColor
                    Layout.alignment: Qt.AlignHCenter
                    visible: timedSamplingPage.isSampling
                }
            }
        }

        // 控制按钮
        RowLayout {
            Layout.alignment: Qt.AlignHCenter
            spacing: 20

            CustomButton {
                text: timedSamplingPage.isSampling ? "停止采样" : "开始采样"
                backgroundColor: timedSamplingPage.isSampling ? AppStyle.dangerColor : AppStyle.primaryColor
                onClicked: {
                    if (timedSamplingPage.isSampling) {
                        stopSampling()
                    } else {
                        startSampling()
                    }
                }
            }

            CustomButton {
                text: "返回"
                backgroundColor: AppStyle.secondaryColor
                onClicked: {
                    if (qmlBridge) {
                        qmlBridge.navigateToPage("Dashboard")
                    }
                }
            }
        }

        Item {
            Layout.fillHeight: true
        }
    }

    // 采样计时器
    Timer {
        id: samplingTimer
        interval: 1000 // 1秒
        running: timedSamplingPage.isSampling
        repeat: true
        onTriggered: {
            timedSamplingPage.elapsedTime++
            if (timedSamplingPage.elapsedTime >= timedSamplingPage.samplingDuration * 60) {
                stopSampling()
            }
        }
    }

    function startSampling() {
        if (qmlBridge && qmlBridge.startTimedSampling(
            durationSpinBox.value,
            flowRateSpinBox.value,
            locationComboBox.currentText,
            remarksField.text
        )) {
            timedSamplingPage.isSampling = true
            timedSamplingPage.samplingDuration = durationSpinBox.value
            timedSamplingPage.elapsedTime = 0
            console.log("定时采样开始")
        } else {
            console.log("启动定时采样失败")
        }
    }

    function stopSampling() {
        if (qmlBridge) {
            qmlBridge.stopSampling()
        }
        timedSamplingPage.isSampling = false
        timedSamplingPage.elapsedTime = 0
        console.log("定时采样停止")
    }
}